package dao

import (
	"context"
	"gorm.io/gorm"
)

type FeedPullEvent struct {
	Id      int64  `gorm:"primaryKey,autoIncrement"`
	Uid     int64  `gorm:"type:int(11)"`
	Type    string `gorm:"type:varchar(255)"`
	Content string `gorm:"type:text"`

	Ctime int64 `gorm:"index"`
}

func (FeedPullEvent) TableName() string {
	return "feed_pull_event"
}

type FeedPullEventDAO interface {
	CreatePullEvents(ctx context.Context, fp FeedPullEvent) error
	FindPullEventList(ctx context.Context, uid []int64, timestamp, limit int64) ([]FeedPullEvent, error)
	FindPullEventListWithTyp(ctx context.Context, typ string, uids []int64, timestamp,
		limit int64) ([]FeedPullEvent, error)
}
type feedPullEventDAO struct {
	db *gorm.DB
}

func NewFeedPullEventDAO(db *gorm.DB) FeedPullEventDAO {
	return &feedPullEventDAO{db: db}
}

func (f feedPullEventDAO) CreatePullEvents(ctx context.Context, fp FeedPullEvent) error {
	return f.db.WithContext(ctx).Create(&fp).Error
}
func (f feedPullEventDAO) FindPullEventList(ctx context.Context, uids []int64, timestamp,
	limit int64) ([]FeedPullEvent, error) {
	var fp []FeedPullEvent
	err := f.db.WithContext(ctx).Where("uid in ? and ctime>?", uids, timestamp).Limit(int(limit)).
		Find(&fp).Error
	return fp, err
}
func (f feedPullEventDAO) FindPullEventListWithTyp(ctx context.Context, typ string, uids []int64, timestamp,
	limit int64) ([]FeedPullEvent, error) {
	var fp []FeedPullEvent
	err := f.db.WithContext(ctx).Where("uid in ? and ctime>? and type=?", uids, timestamp, typ).
		Limit(int(limit)).Find(&fp).Error
	return fp, err
}
